package org.geometerplus.zlibrary.ui.android.view.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;

import com.example.administrator.fangbook.read.view.readView.ZLAndroidWidget;

import org.geometerplus.zlibrary.core.view.ZLViewEnums;


/**
 * Created by Loy on 16-1-23.
 */
public abstract class BaseAnimationProvider {

    /**
     * 绘制w、h
     */
    protected int mWidth = -1, mHeight = -1;

    /**
     * 当前三个页面bitmap（上一页、当前页、下一页）
     */
    protected Bitmap mPrevPageBitmap, mCurrentPageBitmap, mNextPageBitmap;

    /**
     * 动画状态--闲置
     */
    protected AnimationState mState = AnimationState.is_idle;
    /**
     * 动画管理
     */
    protected AnimationManager myAnimationMgr;

    /**
     * 阅读view
     */
    protected ZLAndroidWidget mWidget;


    public BaseAnimationProvider(ZLAndroidWidget widget, AnimationManager mgr) {
        mWidget = widget;
        myAnimationMgr = mgr;
//        myPaint = new Paint();
//        myPaint.setDither(false);
//        myPaint.setFilterBitmap(false);
    }

    /**
     * 设置绘制w、h
     *
     * @param w
     * @param h
     */
    public void setSize(int w, int h) {
        if (mWidth != w || mHeight != h) {
            mWidth = w;
            mHeight = h;
        }
    }

    /**
     * 重置状态
     */
    public void close() {
        mState = AnimationState.is_idle;
        myDirection = PageDirection.Direction_none;
    }

    /**
     * 清空所有加载的图像
     */
    public void resetAllBitmapCache() {
        mCurrentPageBitmap = null;
        mPrevPageBitmap = null;
        mNextPageBitmap = null;
    }

    /**
     * 绘制
     *
     * @param canvas
     */
    public abstract void onDraw(Canvas canvas);

    /**
     * 绘制静态的（最终绘制）
     *
     * @param canvas
     */
    protected void drawStatic(Canvas canvas) {
        //当前的bitmap不为null，但是被回收了
        if (mCurrentPageBitmap != null && mCurrentPageBitmap.isRecycled()) {
            mCurrentPageBitmap = null;
        }
        if (mCurrentPageBitmap == null) {
            //准备数据
            preparePaintInfo();
        }
        //最终绘制
        if (mCurrentPageBitmap != null) {
            canvas.save();
            canvas.drawBitmap(mCurrentPageBitmap, 0, 0, myPaint);
            canvas.restore();
        }
    }

    /**
     * 子类准备信息
     */
    protected abstract void preparePaintInfo();

    /**
     * 父类统一整理（当前页面信息）
     */
    protected void prepareCurrentPagePaintInfo() {
        myAnimationMgr.prepareCurrentPagePaintInfo();
    }

    /**
     * 获取当前的图像
     *
     * @param index
     * @return
     */
    protected Bitmap getPagePaintedBitmap(ZLViewEnums.PageIndex index) {
        return myAnimationMgr.getBitmapCached(index);
    }

    /**
     * 动画三种状态
     * 运行、暂停、闲置
     */
    public enum AnimationState {
        is_running, is_pausing, is_idle
    }

    /**
     * 无、正、负
     */
    public enum AnimationFling {
        FLING_NONE, FLING_POSITIVE, FLING_NEGATIVE
    }

    /**
     * 页面方向
     * 向前、向后、没有、向前滚、向后滚
     */
    enum PageDirection {
        Direction_forward, Direction_backward, Direction_none, Direction_roll_forward, Direction_roll_backward
    }

    protected Paint myPaint;
    protected PointF mPressed = new PointF();
    protected boolean myIsManualMode;
    protected final static int max_interval_turn_one_page_auto_read_mode = 60000;

    protected PointF mTouched = new PointF(), mFastPosition = new PointF();

    protected PageDirection myDirection = PageDirection.Direction_none;


//    public void start(boolean isManual) {
//        myIsManualMode = isManual;
//        mState = AnimationState.is_running;
//    }


//    public boolean pause() {
//        mState = AnimationState.is_pausing;
//        return true;
//    }
//
//    public boolean resume() {
//        mState = AnimationState.is_running;
//        return true;
//    }
//
//    /**
//     * 清除掉缓存页面信息
//     */
//    public void clearCacheBitmap() {
//        myNextPageBitmap = null;
//        myPrevPageBitmap = null;
//        myCurrentPageBitmap = getPagePaintedBitmap(ZLViewEnums.PageIndex.current);
//    }

//    public boolean onPressDown(int x, int y) {
//
//        mTouched.set(x, y);
//        clearCacheBitmap();
//        pause();
//        return false;
//    }
//
//    public boolean onMove(int x, int y) {
//        return false;
//    }
//
//    public boolean onRelease(AnimationFling fling, int x, int y) {
//        resume();
//        return false;
//    }


//    public int getAutoReadScrollSpeedRate() {
//        int rate = 100 - ReadSettingUtil.getAutoFlipSpeed();
//        if (rate <= 0) {
//            rate = 1;
//        }
//        return rate;
//    }


//
//    protected void prepareNextPagePaintInfo() {
//        myAnimationMgr.prepareNextPagePaintInfo();
//    }
//
//    protected void preparePrevPagePaintInfo() {
//        myAnimationMgr.preparePrevPagePaintInfo();
//    }
//

//    /*
//       返回
//     */
//    protected abstract boolean onDraw(Canvas canvas);
//
//
//    protected abstract boolean doUpdate();
//
//    protected abstract int getScrollSpeed();
//
//    protected abstract int getMaxFrameRate();
//
//    protected void startAutoMode(boolean isForward, int x, int y) {
//
//    }
//
//    protected void stopAutoMode() {
//
//    }
//
//    public AnimationState getState() {
//        return mState;
//    }
//
//    protected void startRollback(boolean isForward) {
//    }
//
//
//    public boolean isRunning() {
//        return mState == AnimationState.is_running;
//    }
//
//    /**
//     * 重新加载图像信息
//     *
//     * @return
//     */
//    protected abstract boolean preparePaintInfo();
//
//
//    public boolean isManualMode() {
//        return myIsManualMode;
//    }
}
